package com.example.freshmendemo;



import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonIOException;

import org.json.JSONException;
import org.json.JSONObject;


public class RegistActivity extends AppCompatActivity {

    private EditText userNameEd;
    private EditText nameEd;
    private EditText emailEd;
    private EditText passwordEd;
    private EditText rePasswordEd;
    private Spinner departmentSp;
    private Spinner majorSp;
    private Button registBtn;
    private String departmentStr;
    private String majorStr;
    private EditText telephoneEd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initEvent();
    }

    private void initView(){
        userNameEd = findViewById(R.id.regist_username_ed);
        nameEd = findViewById(R.id.regist_name_ed);
        emailEd = findViewById(R.id.regist_email_ed);
        telephoneEd = findViewById(R.id.regist_phone_ed);
        passwordEd = findViewById(R.id.regist_password_ed);
        rePasswordEd = findViewById(R.id.regist_repassword_ed);

        departmentSp = findViewById(R.id.regsit_department_sp);
        majorSp = findViewById(R.id.regsit_major_sp);

        registBtn = findViewById(R.id.btn_regist);
    }
    private void initEvent(){

        //注册按钮点击事件
        registBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();  //调用发送网络请求
            }
        });

        //获取部门选项下拉框数据
        departmentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] departmentArray = getResources().getStringArray(R.array.depatrment);
                departmentStr = departmentArray[position].toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        majorSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] majorArray = getResources().getStringArray(R.array.major);
                majorStr = majorArray[position].toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initData(){
        String name = nameEd.getText().toString();
        String username = userNameEd.getText().toString();
        String email = emailEd.getText().toString();
        String telephone = telephoneEd.getText().toString();
        String password = passwordEd.getText().toString();
        String repassword = rePasswordEd.getText().toString();

        if (password.equals(repassword)){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("name",name);
                jsonObject.put("username",username);
                jsonObject.put("email",email);
                jsonObject.put("telephone",telephone);
                jsonObject.put("password",password);
                jsonObject.put("major",majorStr);
                jsonObject.put("department",departmentStr);
                jsonObject.put("role","学生");
            }catch (JsonIOException | JSONException e){
                e.printStackTrace();
            }
            RequestQueue requestQueue = Volley.newRequestQueue(RegistActivity.this);
            String url = "http://106.12.30.77:8080/freshmenapp/user/regist";

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, String.valueOf(jsonObject),
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Toast.makeText(RegistActivity.this,jsonObject.optString("mag"),Toast.LENGTH_SHORT).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Toast.makeText(RegistActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                        }
                    }
            );
            requestQueue.add(jsonObjectRequest);
        }else {
            Toast.makeText(RegistActivity.this,"密码不一致",Toast.LENGTH_SHORT).show();
        }
    }
}